const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

var pageNum = 1;//页码
Page({
	data: {
		chooseid: 1,
		status: '',
		school_name: '',
		teachername: '',
		dateStart: '',
		dateEnd: '',
		dataList:[]
	},
	choose(e) {
		this.setData({
			chooseid: e.currentTarget.dataset.id,
			dataList:[]
		});
		this.getAttendCardList();
	},
	todetail(e) {
		let status = e.currentTarget.dataset.status

		if (status == 'leave' || status == 'Supplementary') {
			wx.navigateTo({
				url: '../approval/detail?status=' + status,
			})
		}
	},
	//搜索
	searchFun(e) {
		var teachername = e.detail.value;
		this.setData({
			chooseid: 1,
			teachername: teachername,
      dataList:[],
      total:0
		});
		this.getAttendCardList();
	},
	onReachBottom() {
		// 加载更多，滑动到底部执行此方法
		wx.showLoading({
		  title: '玩命加载中...',
    });
    if(this.data.dataListTotal>=this.data.total){
      wx.showToast({
        icon:"none",
        title: '没有更多数据~',
        duration:2000
      })
    }else{
      pageNum += 1;
      this.getAttendCardList(pageNum);
    }
    wx.hideLoading();
	
	},
	onLoad(options) {
		this.getAttendCardList(1);
	},
	getAttendCardList(page) {
		var that = this;
		http.httpPost(config.getAttendCardList, {
			atype: that.data.chooseid,
			teachername: that.data.teachername,
			dateStart: that.data.dateStart,
			dateEnd: that.data.dateEnd,
			page:page
		}, function(e) {
			if (e.code == 1) {
				if (e.data != []) {
					that.setData({
						// dataList: e.data.list,
						dataList:that.data.dataList.concat(e.data.list),
            school_name: e.data.school_name,
            total:e.data.total, //总条数
            dataListTotal:that.data.dataList.concat(e.data.list).length
					});
				} else { //暂无数据
					wx.showToast({
						title: e.msg,
						icon: 'none',
						duration: 3000,
						mask: true
					});
				}

			}
		});
	},
	bindDateStartChange: function(e) { //选择【开始时间】
		var dateStart = (new Date(e.detail.value)).getTime() / 1000;
		var dateEnd = (new Date(this.data.dateEnd)).getTime() / 1000;
		var flag = this.checkTime(dateStart, dateEnd);

		if (flag) {
			dateStart = e.detail.value;
		} else {
			dateStart = "";
		}

		this.setData({
			dateStart: dateStart
		});

		if (flag) {
			this.setData({
				dataList: []
			});
			this.getAttendCardList();
		}
	},
	bindDateEndChange: function(e) { //选择【结束时间】
		var dateStart = (new Date(this.data.dateStart)).getTime() / 1000;
		var dateEnd = (new Date(e.detail.value)).getTime() / 1000;

		var flag = this.checkTime(dateStart, dateEnd);
		if (flag) {
			dateEnd = e.detail.value;
		} else {
			dateEnd = "";
		}

		this.setData({
			dateEnd: dateEnd
		});

		if (flag) {
			this.setData({
				dataList: []
			});
			this.getAttendCardList();
		}

	},
	checkTime(dateStart, dateEnd) { //校验时间
		var flag = false;
		if (dateStart > dateEnd) {
			wx.showToast({
				icon: 'none',
				title: '开始时间 > 结束时间，请重新选择',
				duration: 2000
			});
		} else {
			flag = true;
		}
		return flag;
	},
	onReady() {
		if (!app.login()) {
			return;
		}
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
